<?php
namespace Wechat\Controller;
use Think\Controller;
require_once(APP_PATH.'/Common/Api/Wechat.php');
class WechatController extends Controller {
	protected $config;
	protected $wechat;
	public function _initialize(){
		$this -> getConfig();
		$wechat = new \WeChat();
		$appid = $this->config['appid'];
		$appsecret = $this ->config['appsecret'];
		$wechat ->setAppId($appid);
		$wechat ->setAppSecret($appsecret);
		$access_token = $this ->config['access_token'];
		$time = time();
		$access_token_time = $this -> config['access_token_time'];
		if($time - $access_token_time>=7200){
			$access_token = $wechat ->gettoken();
			M('Config') ->where(array('key'=>'access_token'))->setField('value',$access_token);
    		M('Config') ->where(array('key'=>'access_token_time'))->setField('value',time());
		}
		$wechat ->setToken($access_token);
		$this ->wechat = $wechat;
	}
	protected function getConfig(){
	   $arr = M('Config')->select();
    	foreach ($arr as $k=>$v){
    		$config[$v['key']] = $v['value']; 
    	}
    	$this ->config = $config;
	} 
	public function index(){}
}